package com.glory.dgcpcpda.activity.mfg;

import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.activity.cpc.CpcDaQtyUpdatedActivity;
import com.glory.dgcpcpda.adapter.PackageCheckAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.Constant;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;
import butterknife.BindView;
import butterknife.OnClick;

public class PackageCheckActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvMainQty)
    TextView tvMainQty;
    @BindView(R.id.tvCustomCode)
    TextView tvCustomCode;
    @BindView(R.id.tvPackageType)
    TextView tvPackageType;
    @BindView(R.id.tvCircuit)
    TextView tvCircuit;
    @BindView(R.id.rvPackageList)
    RecyclerView rvPackageList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private PackageCheckAdapter mPackageCheckAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_package_check;
    }

    @Override
    protected void initView() {
        tvTitle.setText("包装材料过程中校验");
        setMainLayout(llMain);
        initRecyclerView(rvPackageList, this);
        mPackageCheckAdapter = new PackageCheckAdapter(null);
        rvPackageList.setAdapter(mPackageCheckAdapter);

        initEdit(etLot, s -> {
            mPackageCheckAdapter.setNewData(null);

            FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                    (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        tvMainQty.setText(response.body().getResponse().getBody().getLOT().getMAINQTY());
                        tvCustomCode.setText(response.body().getResponse().getBody().getLOT().getCUSTOMERCODE());
                        tvPackageType.setText(response.body().getResponse().getBody().getLOT().getMAINMATTYPE());
                        tvCircuit.setText(response.body().getResponse().getBody().getPRODUCTMODEL());
                        mPackageCheckAdapter.addData(response.body().getResponse().getBody());
                    }
                }
                @Override
                public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                    super.onError(response);
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "PACKINGPROCESS");
            map.addProperty("LOTID", getText(etLot));
            NetUtils.requestPostNet("CPC.PACKINGPROCESS", "TRANSFERMES",this, map, callback);

        });

    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {
        if (mPackageCheckAdapter.getData().size() == 0){
            showToast("请扫描批次号",false);
            return;
        }
        if (mPackageCheckAdapter.getData().get(0).getVALUE().equals("")){
            showToast("请扫描物料批次",false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<JsonObject>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<JsonObject>>
                (new TypeToken<BaseResponseBean<JsonObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JsonObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showToast("校验成功",true);
                    etLot.setText("");
                    tvMainQty.setText("");
                    tvCustomCode.setText("");
                    tvPackageType.setText("");
                    tvCircuit.setText("");
                    mPackageCheckAdapter.setNewData(null);
                }
            }
            @Override
            public void onError(Response<BaseResponseBean<JsonObject>> response) {
                super.onError(response);
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "PACKINGPROCESS");
        map.addProperty("LOTID", getText(etLot));
        map.addProperty("MATERIALBATCH", mPackageCheckAdapter.getData().get(0).getVALUE());
        NetUtils.requestPostNet("CPC.PACKINGPROCESS", "TRANSFERMES",this, map, callback);

    }

}
